/**
 * Copyright 2022 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MINDSPORE_NNACL_FP32_BIAS_ADD_AVX512_H_
#define MINDSPORE_NNACL_FP32_BIAS_ADD_AVX512_H_

#include "nnacl/intrinsics/ms_simd_instructions.h"
#include "nnacl/intrinsics/ms_simd_avx512_instructions.h"

#ifdef __cplusplus
extern "C" {
#endif
#pragma GCC push_options
#pragma GCC target("avx512f")
#define MS_SIMD_INSTRUCTION MS_SIMD_AVX512_INSTRUCTION
#define BLOCK_NUM 16
#define MS_SIMD_AVX512

static inline int BiasAddByInnerCoreAVX512(int index, const float *input, const float *bias, float *output,
                                                       int64_t num) {
  for (int block_max_size = num - BLOCK_NUM + 1; index < block_max_size; index += BLOCK_NUM) {
    SIMD_F32 vin0 = SIMD_LD_F32(input + index);
    SIMD_F32 vin1 = SIMD_LD_F32(bias + index);
    SIMD_F32 vout = SIMD_ADD_F32(vin0, vin1);
    SIMD_ST_F32(output + index, vout);
  }
  return index;
}

static inline int BiasAddByBatchCoreAVX512(int index, const float *input, const float *bias, float *output1,
                                                       float *output2, float *output3, float *output4, int64_t num) {
  for (int block_max_size = num - BLOCK_NUM + 1; index < block_max_size; index += BLOCK_NUM) {
    SIMD_LDX4_F32(input_data, input + index, num);
    SIMD_F32 bias_data = SIMD_LD_F32(bias + index);
    SIMD_ST_F32(output1 + index, SIMD_ADD_F32(input_data1, bias_data));
    SIMD_ST_F32(output2 + index, SIMD_ADD_F32(input_data2, bias_data));
    SIMD_ST_F32(output3 + index, SIMD_ADD_F32(input_data3, bias_data));
    SIMD_ST_F32(output4 + index, SIMD_ADD_F32(input_data4, bias_data));
  }
  return index;
}

#undef MS_SIMD_INSTRUCTION
#undef BLOCK_NUM
#pragma GCC pop_options
#undef MS_SIMD_AVX512
#ifdef __cplusplus
};
#endif

#endif  // MINDSPORE_NNACL_FP32_BIAS_ADD_SIMD_H_
